package com.cn.kehong.service;

import com.cn.kehong.api.IProductDianciFenjService;
import com.cn.kehong.api.IProductPackageService;
import com.cn.kehong.dao.ProductDianciFenjMapper;
import com.cn.kehong.domain.ProductDianciFenj;
import com.cn.kehong.domain.ProductDianciFenjExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ProductDianciFenjServiceImpl implements IProductDianciFenjService {

    @Autowired
    private ProductDianciFenjMapper productDianciFenjMapper;

    @Autowired
    private IProductPackageService productPackageService;


    @Override
    public List<ProductDianciFenj> getProductDianciFenjList(QueryForm queryForm) throws Exception {
        return null;
    }

    @Override
    public List<ProductDianciFenj> getProductDianciFenjListByStatus(QueryForm queryForm) throws Exception {
        String factoryName = queryForm.getFactoryName();
        String status = queryForm.getStatus();
        if(StringUtils.isEmpty(factoryName)||StringUtils.isEmpty(status)){
            return Lists.newArrayList();
        }
        List<ProductDianciFenj> fenjs = productDianciFenjMapper.selectDianciFenjiByStatus(queryForm);
        return fenjs;
    }

    @Override
    public List<ProductDianciFenj> getProductDianciFenjListWithPackage(QueryForm queryForm) throws Exception {
        String factoryName = queryForm.getFactoryName();
        String jizhuCode = queryForm.getJizuNum();
        String packageRoom = queryForm.getPackageRoom();
        if(StringUtils.isEmpty(factoryName)||StringUtils.isEmpty(jizhuCode)||StringUtils.isEmpty(packageRoom)){
            return Lists.newArrayList();
        }
        String splits[] = jizhuCode.split("/");
        int firstJz = Integer.parseInt(splits[0])+4*(Integer.parseInt(packageRoom)-1);
        int secondJz = Integer.parseInt(splits[1])+4*(Integer.parseInt(packageRoom)-1);
        String firstJzStr = firstJz>9?firstJz+"":"0"+firstJz;
        String secondJzStr = secondJz>9?secondJz+"":"0"+secondJz;
        String jizu = firstJzStr+"-"+secondJzStr;
        List<String> jizhus = Lists.newArrayList();
        if(StringUtils.equals("01-02",jizu)){
            jizhus.add("01-02");
        }else if(StringUtils.equals("03-04",jizu)){
            jizhus.add("03");
            jizhus.add("04-05");
        }else if(StringUtils.equals("05-06",jizu)){
            jizhus.add("06-07");
        }else if(StringUtils.equals("07-08",jizu)){
            jizhus.add("08");
            jizhus.add("09-10");
        }else if(StringUtils.equals("09-10",jizu)){
            jizhus.add("11-12");
        }else if(StringUtils.equals("11-12",jizu)){
            jizhus.add("13");
            jizhus.add("14-15");
        }
        queryForm.setJizuNums(jizhus);
        queryForm.setJizuNum(null);
        List<ProductDianciFenj> productDianciFenjs = productDianciFenjMapper.selectDianciFenjiByStatus(queryForm);
        if(CollectionsUtil.isEmpty(productDianciFenjs)){
            return Lists.newArrayList();
        }
        List<String> ids = Lists.newArrayList();
        for(ProductDianciFenj fenj:productDianciFenjs){
            ids.add(fenj.getId());
        }
        Map<String,Integer> jobMaps = productPackageService.getProductPackageJobByFenjiIds(ids);
        for(ProductDianciFenj fenj:productDianciFenjs){
            String id = fenj.getId();
            if(jobMaps.containsKey(id)){
                fenj.setFenjiChooseCount(jobMaps.get(id));
            }else{
                fenj.setFenjiChooseCount(0);
            }
        }
        return productDianciFenjs;
    }

    @Override
    public String addProductDianciFenj(ProductDianciFenj productDianciFenj) throws Exception {
        String dianciId = productDianciFenj.getDianciId();
        if(StringUtils.isEmpty(dianciId)){
            return "缺少dianciid";
        }

        ProductDianciFenjExample example = new ProductDianciFenjExample();
        example.createCriteria().andDianciIdEqualTo(dianciId).andStatusEqualTo(Constants.NORMAL_STATUS).andFenjiCodeEqualTo(productDianciFenj.getFenjiCode());
        int count = productDianciFenjMapper.countByExample(example);
        if(count > 0){
            return "分级机已经存在，无法再次添加";
        }

//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String startTime = simpleDateFormat.format(new Date());
        productDianciFenj.setFenjiStartTime(productDianciFenj.getFenjiStartTime());
        productDianciFenj.setId(CommonUtil.generateRandomNum("fenji-"));
        productDianciFenj.setFenjiStatus("1");
        count = productDianciFenjMapper.insertSelective(productDianciFenj);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateEndProductDianciFenj(ProductDianciFenj productDianciFenj) throws Exception {
        if(StringUtils.isEmpty(productDianciFenj.getId())){
            return "缺少id";
        }
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String endTime = simpleDateFormat.format(new Date());
        ProductDianciFenj update = new ProductDianciFenj();
        update.setFenjiEndTime(productDianciFenj.getFenjiEndTime());
        update.setFenjiStatus("2");
        update.setId(productDianciFenj.getId());
        ProductDianciFenjExample example = new ProductDianciFenjExample();
        example.createCriteria().andIdEqualTo(productDianciFenj.getId());
        int count = productDianciFenjMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }
}
